package com.hyk.commonLib.common.dialogView;

import android.content.Context;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.databinding.Bindable;

import com.hyk.commonLib.common.dialogView.base.BottomPopupViewExt;
import com.hyk.commonLib.common.utils.eventHandler.ViewEventHandler;
import com.hyk.commonLib.common.utils.viewModel.BaseObservableModel;
import com.hyk.commonLib.databinding.CommonLibDialogViewBottomTimeSelectorBinding;

import org.joda.time.DateTime;

import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

public class BottomTimeSelectorDialog extends BottomPopupViewExt<CommonLibDialogViewBottomTimeSelectorBinding> {
    public BottomTimeSelectorDialog(@NonNull Context context) {
        super(context);
        BottomTimeSelectorDialogModel model = new BottomTimeSelectorDialogModel();
        BottomTimeSelectorDialogEventHandler handler = new BottomTimeSelectorDialogEventHandler(this, model);

        getDataBinding().setModel(model);
        getDataBinding().setHandler(handler);

        List<String> hourList = IntStream.range(0, 24).boxed().map(i -> String.format(Locale.CHINA, "%02d", i)).collect(Collectors.toList());
        getDataBinding().whlHour.setDataItems(hourList);
        List<String> minuteList = IntStream.range(0, 60).boxed().map(i -> String.format(Locale.CHINA, "%02d", i)).collect(Collectors.toList());
        getDataBinding().whlMinute.setDataItems(minuteList);
        List<String> secondList = IntStream.range(0, 60).boxed().map(i -> String.format(Locale.CHINA, "%02d", i)).collect(Collectors.toList());
        getDataBinding().whlSecond.setDataItems(secondList);
    }

    @Getter(onMethod_ = {@Bindable})
    @Setter(onMethod_ = {@BaseObservableModel.PropertiesChanged(value = true)})
    @Accessors(chain = true)
    public static class BottomTimeSelectorDialogModel extends BaseObservableModel {
        @Getter(AccessLevel.NONE)
        @Setter(AccessLevel.NONE)
        private DateTime selectTime = DateTime.now();
        @Setter
        private OnTimeSelectListener onTimeSelectListener;
        @Setter
        @Getter
        private String timeFormatter = "HH:mm:ss";
        @TimeSelectType
        @Setter(onMethod_ = {@BaseObservableModel.PropertiesChanged(value = true)}, onParam_ = {@TimeSelectType})
        private int selectType = TimeSelectType.ALL;

        @Bindable("selectType")
        public boolean isShowHour() {
            return selectType == TimeSelectType.ALL || selectType == TimeSelectType.HOUR_MINUTE;
        }

        @Bindable("selectType")
        public boolean isShowMinute() {
            return selectType == TimeSelectType.ALL || selectType == TimeSelectType.HOUR_MINUTE || selectType == TimeSelectType.MINUTE_SECOND;
        }

        @Bindable("selectType")
        public boolean isShowSecond() {
            return selectType == TimeSelectType.ALL || selectType == TimeSelectType.MINUTE_SECOND;
        }

        @Bindable
        public int getSelectHour() {
            return selectTime.getHourOfDay();
        }

        @Bindable
        public int getSelectMinute() {
            return selectTime.getMinuteOfHour();
        }

        @Bindable
        public int getSelectSecond() {
            return selectTime.getSecondOfMinute();
        }

        @BaseObservableModel.PropertiesChanged(names = {"selectHour", "selectMinute", "selectSecond"})
        public BottomTimeSelectorDialogModel setSelectTime(int hour, int minute, int second) {
            selectTime = selectTime.withHourOfDay(hour).withMinuteOfHour(minute).withSecondOfMinute(second);
            return this;
        }
    }

    public static class BottomTimeSelectorDialogEventHandler extends ViewEventHandler<BottomTimeSelectorDialog, BottomTimeSelectorDialog.BottomTimeSelectorDialogModel> {

        public BottomTimeSelectorDialogEventHandler(@NonNull BottomTimeSelectorDialog view, BottomTimeSelectorDialog.BottomTimeSelectorDialogModel model) {
            super(view, model);
        }

        public void onTimeSelected() {
            if (getModel().onTimeSelectListener != null) {
                BottomTimeSelectorDialogModel model = getModel();
                int hour = Integer.parseInt((String) getContainer().getDataBinding().whlHour.getSelectedItemData());
                int minute = Integer.parseInt((String) getContainer().getDataBinding().whlMinute.getSelectedItemData());
                int second = Integer.parseInt((String) getContainer().getDataBinding().whlSecond.getSelectedItemData());
                switch (model.selectType) {
                    case TimeSelectType.ALL:
                        break;
                    case TimeSelectType.HOUR_MINUTE:
                        second = 0;
                        break;
                    case TimeSelectType.MINUTE_SECOND:
                        hour = 0;
                        break;
                }
                DateTime dateTime = new DateTime().withHourOfDay(hour).withMinuteOfHour(minute).withSecondOfMinute(second);
                model.onTimeSelectListener.onSelect(dateTime.toString(model.getTimeFormatter()), hour, minute, second);
            }
            getContainer().dismiss();
        }
    }

    public interface OnTimeSelectListener {
        void onSelect(String time, int hour, int minute, int second);
    }

    @IntDef({
            TimeSelectType.ALL,
            TimeSelectType.HOUR_MINUTE,
            TimeSelectType.MINUTE_SECOND,
    })
    public @interface TimeSelectType {
        int ALL = 1;
        int HOUR_MINUTE = 2;
        int MINUTE_SECOND = 3;
    }
}